package it.jack;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import database.DB;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.*;

import oggetti.Utente;

public class Main extends JFrame {

    private static final long serialVersionUID = 7753538273775773761L;
    private static Main instance;
    private Utente user;
    private JDesktopPane jDesktopPane1;

    {
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("static-access")
    public Main() {
        super("Login");
        jDesktopPane1 = new JDesktopPane();
        jDesktopPane1.setOpaque(false);
        this.add(jDesktopPane1,BorderLayout.CENTER);
        initGUI();
        this.instance = this;
        addWindowsListener();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    public void addInDesktop(JInternalFrame frame) {
        try {
            jDesktopPane1.setSize(Toolkit.getDefaultToolkit().getScreenSize());
            jDesktopPane1.add(frame);
            frame.moveToFront();
            frame.setSelected(true);
            frame.setVisible(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public void setUser(Utente user) {
        this.user = user;
    }

    public Utente getUser() {
        return this.user;
    }

    private void initGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setSize(300, 300);
        centra();
        this.add(new Login(), BorderLayout.CENTER);
        setVisible(true);
    }

    private void addWindowsListener() {
        this.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent arg0) {
                if (JOptionPane.showConfirmDialog(null, "Sei sicuro di voler uscire?", "Uscita", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });
    }

    public static Main getInstance() {
        return instance;
    }

    public void centra() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(new Point((dimension.width - getSize().width) / 2,
                (dimension.height - getSize().height) / 2));
    }
    public void disableDesktopPane(){
       jDesktopPane1.setSize(0,0);    
    }
    public static void main(String[] args) throws Exception {
        new Main();
    }
}
